﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TaskManagerUserBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEquipmentProfileDA equipmentProfileDA = DataAccess.CreateEquipmentProfileDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITaskManagerUserDA taskManagerUserDA = DataAccess.CreateTaskManagerUserDA();
        private static readonly ITaskManagerUserDetailDA taskManagerUserDetailDA = DataAccess.CreateTaskManagerUserDetailDA();
        private static readonly ITaskRuleDA taskRuleDA = DataAccess.CreateTaskRuleDA();
        private static readonly IUserMstDA userMstDA = DataAccess.CreateUserMstDA();

        public TaskManagerUserBLL()
        {
            this.pubParasInfo = null;
        }

        public TaskManagerUserBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TaskManagerUserBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteTaskManagerUser(List<string> taskManagerUserID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string whLoginID = this.dataBaseID[0];
            string temp = string.Empty;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                List<TaskManagerUserInfo> taskManagerUserInfoList = new List<TaskManagerUserInfo>();
                TaskManagerUserInfo taskManagerUserInfo = null;
                foreach (string id in taskManagerUserID)
                {
                    taskManagerUserInfo = taskManagerUserDA.GetTaskManagerUserByID(dataBase, tran, id, this.dataBaseID[0]);
                    taskManagerUserInfoList.Add(taskManagerUserInfo);
                }
                taskManagerUserDetailDA.DeleteTaskManagerUserDetail(dataBase, tran, taskManagerUserID, this.dataBaseID[0]);
                if (taskManagerUserDA.DeleteTaskManagerUser(dataBase, tran, taskManagerUserID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < taskManagerUserID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TaskManagerUserInfo>(dataBase, tran, taskManagerUserInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_MANAGER_USER", this.dataBaseID[i]);
                        temp = temp + taskManagerUserID[i] + ",";
                    }
                }
                tran.Commit();
                temp = temp.Substring(0, temp.Length - 1);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllTaskManagerUser()
        {
            return taskManagerUserDA.GetAllTaskManagerUser(this.dataBaseID[0]);
        }

        public TaskManagerUserInfo GetTaskManagerUserByID(string taskdetailID)
        {
            TaskManagerUserInfo taskManagerUserInfo = taskManagerUserDA.GetTaskManagerUserByID(taskdetailID, this.dataBaseID[0]);
            if (taskManagerUserInfo != null)
            {
                taskManagerUserInfo.TaskManagerUserDetailInfos = taskManagerUserDetailDA.GetTaskManagerUserDetailByID(taskManagerUserInfo.UserID, this.dataBaseID[0], this.pubParasInfo);
            }
            return taskManagerUserInfo;
        }

        public DataSet GetTaskManagerUserByQueryList(TaskManagerUserQueryEntity taskdetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(taskdetailQuery.UserID))
            {
                sqlWhere.Add(" USER_ID " + taskdetailQuery.UserID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.UserGroup))
            {
                sqlWhere.Add(" USER_GROUP " + taskdetailQuery.UserGroup);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.EquipmentProfileID))
            {
                sqlWhere.Add(" EQUIPMENT_PROFILE_ID " + taskdetailQuery.EquipmentProfileID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.TaskStrategyID))
            {
                sqlWhere.Add(" TASK_STRATEGY_ID " + taskdetailQuery.TaskStrategyID);
            }
            return taskManagerUserDA.GetTaskManagerUserByQueryList(sqlWhere, taskdetailQuery, this.dataBaseID[0]);
        }

        public string InsertTaskManagerUser(TaskManagerUserInfo taskManagerUserDAInfo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!userMstDA.CheckUserMstIDUnique(dataBase, tran, taskManagerUserDAInfo.UserID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_001", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.UserID });
                }
                if (taskManagerUserDA.CheckTaskManagerUserIDUnique(dataBase, tran, taskManagerUserDAInfo.UserID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_002", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.UserID });
                }
                if (!string.IsNullOrEmpty(taskManagerUserDAInfo.TaskStrategyID) && !taskRuleDA.CheckTaskRuleIDUnique(dataBase, tran, taskManagerUserDAInfo.TaskStrategyID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_004", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.TaskStrategyID });
                }
                if (!string.IsNullOrEmpty(taskManagerUserDAInfo.EquipmentProfileID) && !equipmentProfileDA.CheckEquipmentProfileIDUnique(dataBase, tran, taskManagerUserDAInfo.EquipmentProfileID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_005", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.EquipmentProfileID });
                }
                taskManagerUserDAInfo.CreatedDate = DateTime.Now;
                taskManagerUserDAInfo.UpdatedDate = DateTime.Now;
                taskManagerUserDAInfo.UpdatedBy = taskManagerUserDAInfo.CreatedBy;
                taskManagerUserDA.InsertTaskManagerUser(dataBase, tran, taskManagerUserDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskManagerUserInfo>(dataBase, tran, null, taskManagerUserDAInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TASK_MANAGER_USER", this.dataBaseID[0]);
                tran.Commit();
                temp = taskManagerUserDAInfo.UserID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string UpdateTaskManagerUser(TaskManagerUserInfo taskManagerUserDAInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!userMstDA.CheckUserMstIDUnique(dataBase, tran, taskManagerUserDAInfo.UserID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_001", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.UserID });
                }
                if (!string.IsNullOrEmpty(taskManagerUserDAInfo.TaskStrategyID) && !taskRuleDA.CheckTaskRuleIDUnique(dataBase, tran, taskManagerUserDAInfo.TaskStrategyID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_004", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.TaskStrategyID });
                }
                if (!string.IsNullOrEmpty(taskManagerUserDAInfo.EquipmentProfileID) && !equipmentProfileDA.CheckEquipmentProfileIDUnique(dataBase, tran, taskManagerUserDAInfo.EquipmentProfileID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6040_005", this.pubParasInfo.UserLanguage, new List<string> { taskManagerUserDAInfo.EquipmentProfileID });
                }
                TaskManagerUserInfo oldTaskManagerUserInfo = taskManagerUserDA.GetTaskManagerUserByID(dataBase, tran, taskManagerUserDAInfo.UserID, this.dataBaseID[0]);
                taskManagerUserDA.UpdateTaskManagerUser(dataBase, tran, taskManagerUserDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskManagerUserInfo>(oldTaskManagerUserInfo, taskManagerUserDAInfo, AuditAction.Update, this.pubParasInfo.UserID, "TASK_MANAGER_USER", this.dataBaseID[0]);
                tran.Commit();
                return taskManagerUserDAInfo.UserID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

